package com.go.where.manager.service.system.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.go.where.manager.dao.system.OperatorDao;
import com.go.where.manager.dao.system.MenuDao;
import com.go.where.manager.exception.ManagerException;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.user.OperatorInfo;
import com.go.where.manager.pojo.user.OperatorVO;
import com.go.where.manager.service.system.OperatorService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.KeyUtil;

@Service("operatorService")
public class OperatorServiceImpl implements OperatorService {

	private static Logger log = Logger.getLogger(OperatorServiceImpl.class);
	@Resource(name = "operatorDao")
	private OperatorDao operatorDao;
	@Resource(name = "menuDao")
	private MenuDao menuDao;

	@Override
	public OperatorInfo login(OperatorVO operator, String ip) {
		String userId = operator.getUserId();
		String password = operator.getPassword();
		if (userId == null || password == null) {
			throw new ManagerException("登录用户名或密码错误.");
		}
		password = KeyUtil.passwordEncrypt(password);
		OperatorInfo op = this.operatorDao.queryByAccount(userId);
		if (op == null || !password.equals(op.getPassword())) {
			throw new ManagerException("登录用户名或密码错误.");
		}
		List<String> codes = null;
		// 如果是admin用户则返回全部权限
		if (StringUtils.isNotBlank(op.getIsRoot())
				&& op.getIsRoot().equals("1")) {
			codes = menuDao.queryAll();
		} else {
			codes = menuDao.queryByRoleId(op.getRoleId());
		}
		op.setCodes(CommonUtil.gson().toJson(codes));
		operator.setLastLoginIp(ip);
		operator.setLastLoginTime(new Date());
		operator.setPassword("");
		this.operatorDao.updateOperLogin(operator);
		log.info("用户：" + operator.getUserId() + "，登录成功.");
		return op;
	}

	public static void main(String[] args) {
		List<String> codes = new ArrayList<String>();
		codes.add("1321321");
		codes.add("234r343");
		System.out.println(CommonUtil.gson().toJson(codes));

	}

	@Override
	public boolean checkOperatorAccount(String userId) {
		OperatorInfo oop = operatorDao.getByKey(userId);
		if (oop != null) {
			return true;
		}
		return false;
	}

	@Override
	public OperatorInfo queryOperatorInfoByUserId(String userId) {
		return operatorDao.getByKey(userId);
	}

	@Override
	public Pagination<OperatorInfo> queryPage(OperatorInfo operator) {
		Integer count = operatorDao.totalCount(operator);
		operator.calculate();
		List<OperatorInfo> list = operatorDao.queryList(operator);
		Pagination<OperatorInfo> page = operatorDao.queryPagination(count,
				list, operator);
		return page;
	}

	@Override
	public int save(OperatorInfo operator) {
		String password = KeyUtil.passwordEncrypt(operator.getPassword());
		operator.setPassword(password);
		operator.setCreateTime(new Date());
		operator.setIsRoot("0");
		return operatorDao.insert(operator);
	}

	@Override
	public int update(OperatorInfo operator) {
		operator.setUpdateTime(new Date());
		return operatorDao.update(operator);
	}

	@Override
	public int delete(String userId) {
		return operatorDao.deleteByKey(userId);
	}

}
